package com.data.center.util.ForeignManage;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class ForeignManageUtils {

    public static List<ForeignDataItem> foreignDataItemList = new ArrayList<>();

    public void init() {
        foreignDataItemList = new ArrayList<>();
    }

    /**
     * @param foreignEntityName 关联实体名称
     * 获取依赖输入表的所有表的查询/删除/合并方法
     * **/
    public List<ForeignDataItem> getForeignDataItem(String foreignEntityName) {
        log.info("查询" + foreignEntityName + "关联表,关系表中共" + foreignDataItemList.size() + "条数据");
        List<ForeignDataItem> result = new ArrayList<>();
        for (ForeignDataItem foreignDataItem : foreignDataItemList) {
            if (foreignDataItem.foreignEntityName.equals(foreignEntityName)) {
                result.add(foreignDataItem);
            }
        }
        log.info("共查询到" + result.size() + "条依赖关系：" + result);
        return result;
    }

    public void addForeignDataItem(
            String entityName,
            String fieldName,
            String foreignEntityName,
            String foreignKey,
            Object serviceClass,
            Method searchNumByForeignKeyMethod,
            Method deleteByForeignKeyMethod,
            Method mergeByForeignKeyMethod
    ) {
        ForeignDataItem foreignDataItem = new ForeignDataItem(
                entityName,
                fieldName,
                foreignKey,
                foreignEntityName,
                serviceClass,
                searchNumByForeignKeyMethod,
                deleteByForeignKeyMethod,
                mergeByForeignKeyMethod);

        foreignDataItemList.add(foreignDataItem);

        log.info("添加关联数据：{}", foreignDataItem);
    }



}
